package com.farm.wcp.util;

import java.io.File;
import com.farm.authority.FarmAuthorityService;
import com.farm.core.auth.domain.LoginUser;
import com.farm.parameter.FarmParameterService;
import com.farm.parameter.domain.AloneDictionaryEntity;
import com.farm.parameter.service.DictionaryEntityServiceInter;
import com.farm.parameter.service.ParameterServiceInter;
import com.farm.util.spring.BeanFactory;

public class InitFilePathUtil {

	public static void iniPaths() {
		ParameterServiceInter parameterServiceImpl = (ParameterServiceInter) BeanFactory
				.getBean("parameterServiceImpl");

		DictionaryEntityServiceInter dictionaryEntityServiceImpl = (DictionaryEntityServiceInter) BeanFactory
				.getBean("dictionaryEntityServiceImpl");

		LoginUser user = FarmAuthorityService.getInstance().getUserByLoginName("sysadmin");
		if (FarmParameterService.getInstance().getParameter("config.doc.dir").equals("INIT")) {
			System.out.println("初始化目录：config.doc.dir：default:" + getSysFilePath("cbtgfile"));
			parameterServiceImpl.setValue("config.doc.dir", "default:" + getSysFilePath("cbtgfile"), user);
		}
		if (FarmParameterService.getInstance().getParameter("config.doc.file.export").equals("INIT")) {
			System.out.println("初始化目录：config.doc.file.export：" + getSysFilePath("cbtgexport"));
			parameterServiceImpl.setValue("config.doc.file.export", getSysFilePath("cbtgexport"), user);
		}

		if (!dictionaryEntityServiceImpl.isExistEntity("CBTG_CONFIG_CODETITLE")) {
			AloneDictionaryEntity entity = new AloneDictionaryEntity();
			entity.setName("数据表字段TITLE转义");
			entity.setEntityindex("CBTG_CONFIG_CODETITLE");
			entity.setType("1");
			entity.setComments("当解析数据库字段信息时，可以根据字典自动获得title");
			dictionaryEntityServiceImpl.insertEntity(entity, user);
		}

		parameterServiceImpl.refreshCache();
	}

	/**
	 * 生成一个默认目录
	 * 
	 * @param dirname
	 * @return
	 */
	private static String getSysFilePath(String dirname) {
		String tomcatPath = System.getProperty("catalina.home");
		return tomcatPath + File.separator + "resource" + File.separator + dirname;
	}

}
